<template>
  <div>
    <span class="iconfont return" @click="up">&#xe61e;</span>
    <topnav v-bind:title="navTitle"></topnav>
     <div class="pwd">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="oldpass">
          <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import topnav from '@/components/topnav'
import Router from '../../router'
var phone = localStorage.getItem('currentUser_name')
var stats = false
export default {
  components: {
    topnav
  },
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      navTitle: '修改密码',
      ruleForm: {
        oldpass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        oldpass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    up () {
      Router.push('/myset')
    },
    submitForm (formName) {
      if (stats) {
        return ''
      }
      stats = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let obj = this.$qs.stringify({
            mobile: phone,
            oldpwd: this.ruleForm.oldpass,
            newpwd: this.ruleForm.pass,
            repwd: this.ruleForm.checkPass
          })
          this.$axios.post('/api.php/Index/index?action=modifyPassword&' + obj, {
          }).then(res => {
            this.resetForm(formName)
            let msg = res.data.message
            this.$message({
              message: msg,
              type: 'success'
            })
            Router.push('/')
          })
        } else {
          console.log('error submit!!')
          return false
        }
        stats = false
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="stylus" scoped>
 .return
   position absolute
   margin-top 0.8rem
   margin-left 0.5rem
   color #ffffff
 .pwd
  padding 0.25rem
</style>
